import request, { all } from '@/utils/request'
import store from './../store'

/**
 * 子账号列表
 * @param params
 * @returns AxiosPromise
 */
export function accountPaginate(params) {
  params = JSON.parse(JSON.stringify(params))
  return request({
    url: '/merchant/account/paginate',
    method: 'get',
    params: Object.assign(params, { merchant_id: store.getters.merchant.merchant_id })
  })
}

/**
 * 购买子账号
 * @param params
 * @returns AxiosPromise
 */
export function buyAccount(params) {
  return request({
    url: '/merchant/buy/account',
    method: 'post',
    data: params
  })
}

/**
 * 子账号信息
 * @param id
 * @param processList
 * @param callback
 */
export function stepInfo(id, processList = false, callback = null) {
  if (processList) {
    const url = [
      request.get('/merchant/process/step?sub_id=' + id),
      request.get('/process/lists?status=1')
    ]
    all(url, function(res) {
      if (res !== false) {
        callback(res)
      }
    })
  } else {
    return request({
      url: '/merchant/account/info/' + id,
      method: 'get'
    })
  }
}

/**
 * 子账号信息
 * @param id
 * @param callback
 * @param abilityList
 */
export function accountInfo(id, abilityList = false, callback = null) {
  if (abilityList) {
    const url = [
      request.get('/merchant/account/info/' + id),
      request.get('/merchant/ability?format=tree&is_have=1'),
      request.get('/anchor/lists'),
      request.get('/studio/lists')
    ]
    all(url, function(res) {
      if (res !== false) {
        callback(res)
      }
    })
  } else {
    return request({
      url: '/merchant/account/info/' + id,
      method: 'get'
    })
  }
}

/**
 * 绑定子账号
 * @param params
 * @returns AxiosPromise
 */
export function bindAccount(params) {
  return request({
    url: '/merchant/bind/account',
    method: 'post',
    data: params
  })
}

/**
 * 清楚绑定
 * @param sub_id
 * @returns AxiosPromise
 */
export function bindClear(sub_id) {
  return request({
    url: '/merchant/bind/clear',
    method: 'get',
    params: { sub_id: sub_id }
  })
}

/**
 * 功能列表
 * @returns AxiosPromise
 */
export function abilityList(params) {
  return request({
    url: '/merchant/ability',
    method: 'get',
    params: params
  })
}

/**
 * 用户授权
 * @returns AxiosPromise
 */
export function authorize(params) {
  return request({
    url: '/merchant/authorize',
    method: 'post',
    data: params
  })
}

/**
 * 用户授权
 * @returns AxiosPromise
 */
export function processAuthorize(params) {
  return request({
    url: '/merchant/process/authorize',
    method: 'post',
    data: params
  })
}

/**
 * 用户授权
 * @returns AxiosPromise
 */
export function anchorAuthorize(params) {
  return request({
    url: '/merchant/anchor/authorize',
    method: 'post',
    data: params
  })
}

/**
 * 用户授权
 * @returns AxiosPromise
 */
export function studioAuthorize(params) {
  return request({
    url: '/merchant/studio/authorize',
    method: 'post',
    data: params
  })
}

/**
 * 购买功能
 * @returns AxiosPromise
 */
export function buyAbility(params) {
  return request({
    url: '/merchant/buy/ability',
    method: 'post',
    data: params
  })
}

/**
 * 修改密码
 * @param params
 * @returns AxiosPromise
 */
export function changePassword(params) {
  return request({
    url: '/merchant/change/pswd',
    method: 'post',
    data: params
  })
}
